(function($) {
	"use strict";
	$.widget("isperp.processButton", {
		options : {
			id : "",
			buttonId : "divProcessButton",
			setting : null,
			beforeSubmit : null,
			processMode : 0,
			formId : "",
			fromId : "",
			fromProcessKey : "",
			processId : "",
			processKey : "",
			businessKey : "",
			viewOnly : false,
			areHideProcessBtn : false,
			autoEntryNext : undefined,
			isValidate_ : null,
			afterInit : null
		},

		_init : function() {
			var $self = this, o = $self.options, $element = $("#" + o.buttonId);
			var fromId = isperp.getUrlParamByName("fromId"), fromProcessKey = isperp.getUrlParamByName("fromProcessKey");

			if (fromId) {
				o.fromId = fromId;
				o.fromProcessKey = fromProcessKey;
			}

			if (o.viewOnly) {

				var html = "";

				if (o.processId && o.processId != "") {

					html = "<a class='button' href='javascript:void(0)' onclick='javascript:isperp.openDialog({title:\"流程跟踪\",contentUrl:\"workflow/processRun/traceView?processId=" + o.processId + "\"}); return false;'>流程跟踪</a>";

				} else {
					html += "<a class='button'  href='javascript:void(0)' onclick='isperp.openDialog({title:\"查看处理流程图\",contentUrl:\"workflow/processDefinition/traceDefView?processKey=" + o.processKey + "\"}); return false;'>查看处理流程图</a>";

				}

				$element.append(html);
			} else {

				$.ajax({
					type : "GET",
					dataType : "json",
					cache : false,
					url : "workflow/processRun/getNextSteps",
					data : {
						"key" : o.processKey,
						"processId" : o.processId
					},
					success : function(result) {
						var buttons = "", canSave = false;
						var styleStr = "";
						if (o.areHideProcessBtn) {
							styleStr = "style='display:none'";
						}
						if (result) {
							$(result).each(
									function(index, item) {
										canSave = true;
										buttons += "<button class=\"processButton nui-mainbtn\" data-are_open_comment_window=\"" + item.areOpenCommentWindow + "\"  data-are_auto_entry_next=\"" + item.areAutoEntryNext + "\"  data-next_task_id=\"" + item.nextTaskId + "\" data-next_transition_id=\"" + item.nextTransitionId + "\"  data-name=\"" + item.name + "\" data-commit_type=\"" + item.commitType
												+ "\" data-process_mode=\"" + item.processMode + "\" " + styleStr + ">" + item.name + "</button>";
									});
						}
						if (o.processId != "") {
							buttons += "<a class='button' href='javascript:void(0)' onclick='isperp.openDialog({title:\"流程跟踪\",contentUrl:\"workflow/processRun/traceView?processId=" + o.processId + "\"}); return false;'>流程跟踪</a>";
						} else {

							if (o.fromId) {
								$.ajax({
									url : "bpm/relationProcessMap/getRelationMap",
									data : {
										"fromId" : o.fromId,
										"fromProcessKey" : o.fromProcessKey,
										"processKey" : o.processKey
									},
									cache : false,
									success : function(data) {

										$(data).each(function(index, item) {
											var f = $("[name='" + item.field + "']");
											f.val(item.value);
											if (f.is("select"))
												f.trigger("chosen:updated");
										});
										if (o.afterInit) {
											o.afterInit();
										}

									}
								});
							} else {
								if (o.afterInit)
									o.afterInit();
							}

							buttons += "<a class='button'  href='javascript:void(0)' onclick='isperp.openDialog({title:\"查看处理流程图\",contentUrl:\"workflow/processDefinition/traceDefView?processKey=" + o.processKey + "\"}); return false;'>查看处理流程图</a>";
						}
						if (canSave) {

							if (!o.fromId)
								buttons += "<a class='button' href='javascript:void(0)' onclick='isperp.submitForm({isValidate:true});return false;'>暂存</a>";
						}
						$element.append(buttons);

						$("#" + o.buttonId + " .processButton").click(function() {
							var $this = $(this);
							var nextTransitionId = $this.data("next_transition_id"), nextTaskId = $this.data("next_task_id"), name = $this.data("name"), commitType = $this.data("commit_type"), processMode = $this.data("process_mode");
							var suggestion = $this.data("suggestion");
							// alert(suggestion);
							// edit at 20150425
							var isValidateButtom = $("#isValidate");
							var validateButText = isValidateButtom.attr("validateText");
							if (isValidateButtom != undefined && name == validateButText) {
								var validateFun = isValidateButtom.attr("excuseFun");
								var result = eval(validateFun);
								if (result == false) {
									return false;
								}
							}
							var areOpenCommentWindow = $this.data("are_open_comment_window"), areAutoEntryNext = o.autoEntryNext || $this.data("are_auto_entry_next");
							$self.processOnclick(nextTaskId, nextTransitionId, name, commitType, processMode, areOpenCommentWindow, areAutoEntryNext, suggestion);
						});
					}
				});

			}

		},

		_submitProcessLoadingStart : function(o) {
			var $element = $("#" + o.buttonId);
			$element.hide();
			$element.parent().append("<label id='processLoading' style='float:right;font-size:14px;color:red;'>正在提交，请稍后......</lable>");

		},
		_submitProcessLoadingEnd : function(o) {
			var $element = $("#" + o.buttonId);
			$element.show();
			$element.parent().find("#processLoading").remove();

		},
		_submitProcess : function(nextTaskId, nextTransitionId, name, commitType, processMode, areOpenCommentWindow, areAutoEntryNext, suggestion) {

			var $self = this, o = $self.options, $element = $("#" + o.buttonId);

			var pcontrolid = isperp.getUrlParamByName("pcontrolid");
			if (processMode == 2 || commitType != 0 || areOpenCommentWindow) {
				var th = 390, tw = 350;
				if (commitType == 2)
					th = 520, tw = 600;
				isperp.openDialog({
					contentUrl : "workflow/processRun/approvalEdit?pcontrolid=" + pcontrolid + "&key=" + o.processKey + "&nextTaskId=" + nextTaskId + "&nextTransitionId=" + nextTransitionId + "&processId=" + o.processId + "&name=" + name + "&businessKey=" + o.businessKey + "&processMode=" + processMode + "&commitType=" + commitType + "&fromId=" + o.fromId + "&fromProcessKey=" + o.fromProcessKey,
					width : tw,
					height : th,
					title : "提交流程",
					onThisWindow : true,
					modal : true
				});

			} else {
				// 提交到下一步
				$.ajax({
					type : "POST",
					url : "workflow/processRun/gotoNextStep",
					data : {
						"key" : o.processKey,
						"nextTaskId" : nextTaskId,
						"nextTransitionId" : nextTransitionId,
						"processId" : o.processId,
						"businessKey" : o.businessKey,
						"processMode" : processMode,
						"commitType" : commitType,
						"fromId" : o.fromId,
						"fromProcessKey" : o.fromProcessKey,
						"suggestion" : suggestion
					},
					success : function(result) {
						if (result.success) {
							if (areAutoEntryNext) {
								window.location.replace(isperp.webPath + "/workflow/processRun/entry?processKey=" + o.processKey + "&id=" + o.businessKey);
								isperp.reloadParentControl();
								isperp.tooltip("流程提交成功！");
							} else {
								isperp.alert(result.data);
								isperp.reloadParentControl();
								isperp.closeDialog();

							}
						}
					},
					error : function(jqXHR, textStatus, errorThrown) {
						isperp.errorAlert(jqXHR.responseText);
					},
					dataType : "json"
				});
			}

		},
		processOnclick : function(nextTransitionId, nextTaskId, name, commitType, processMode, areOpenCommentWindow, areAutoEntryNext, suggestion) {
			var result = true;
			var $self = this, o = $self.options;
			var isValidate_ = false;
			if (o.beforeSubmit) {
				result = o.beforeSubmit(name);
			}
			if (o.isValidate_) {
				isValidate_ = o.isValidate_(name);
			}
			if (result) {
				$self._submitProcessLoadingStart(o);
				if (o.processMode == 0 && o.formId != "" && processMode != 2) {
					var res = isperp.submitForm({
						formId : o.formId,
						isValidate : isValidate_,
						success : function() {
							$self._submitProcess(nextTransitionId, nextTaskId, name, commitType, processMode, areOpenCommentWindow, areAutoEntryNext, suggestion);
						},
						error : function() {
							isperp.alert("提交失败!");
						}
					});
					if (!res) {
						$self._submitProcessLoadingEnd(o);
					}

				} else {
					$self._submitProcess(nextTransitionId, nextTaskId, name, commitType, processMode, areOpenCommentWindow, areAutoEntryNext, suggestion);
				}
			}
		}

	});
}(jQuery));
